package 
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;
	import ginger.utils.NetUtil;
	import hwsoft.webservice_2_0.utils.ParseUtil;
	import interactive.turnAnimation.ShowType;
	import interactive.turnAnimation.TureEvent;
	import caurina.transitions.Tweener;
	import UILib.LeftBtn;
	import UILib.RightBtn;

	/**
	 * ...
	 * @author Ning
	 */
	[Frame(factoryClass="Preloader")]
	public class Main extends Sprite 
	{
		private var _player:Player;
		private var _leftBtn:LeftBtn;
		private var _rightBtn:RightBtn;
		public function Main():void 
		{
			if (stage) onAddedToStage();
			else addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}

		private function onAddedToStage(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			// entry point
			setTimeout(init, 200);
		}
		private function init():void
		{
			this.stage.align = StageAlign.TOP_LEFT;
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			
			var obj:Object = NetUtil.getFlashVarsParam(this.root, 'data');
			var url:String = obj == null?'asset/data.xml':String(obj);
			
			var urlloader:URLLoader = new URLLoader();
			urlloader.addEventListener(Event.COMPLETE, onComplete);
			urlloader.load(new URLRequest(String(url)));
		}
		private function onComplete(evt:Event):void
		{
			var data:Array = ParseUtil.parse(XML(evt.target.data), PlayerVO);
			_player = new Player(904, 279, data.length, 5,ItemRenderer, ShowType.MIDDLE, 10, .8);
			addChild(_player);
			setTimeout(_player.setData, 500, data);
			
			_leftBtn = new LeftBtn();
			addChild(_leftBtn);
			
			_rightBtn = new RightBtn();
			addChild(_rightBtn);
			
			_leftBtn.x =80;
			_leftBtn.y = 100;
			_rightBtn.x = 760;
			_rightBtn.y = 100;
			
			_leftBtn.alpha = _rightBtn.alpha = 0;
			Tweener.addTween(_leftBtn, {time:2,delay:.5,alpha:1,x:130} );
			Tweener.addTween(_rightBtn, {time:2,delay:.5,alpha:1,x:710} );
			
			_leftBtn.addEventListener(MouseEvent.CLICK, onLeft);
			_rightBtn.addEventListener(MouseEvent.CLICK, onRight);
		}
		private function onLeft(evt:MouseEvent):void
		{
			_player.turnLeft();
		}
		private function onRight(evt:MouseEvent):void
		{
			_player.turnRight();
		}
	}
}